﻿<cfsilent>
	<cfscript>

		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");

		imgObj = getProperty("serviceFactory").getBean("imageObject");
		fileObj = getProperty("serviceFactory").getBean("tagFileHelper");
		contentObj = getProperty("serviceFactory").getBean("tagContentHelper");

		/* 学籍相关信息 */
		userId = sessionAdvice.getAutherUserID();
		userGroups = sessionAdvice.getAuthorizedUserGroups();
			
		/*
			P001	教师
			P002	学生
		*/
		if ( listFind( userGroups, "P001", "," ) ) {
			userType = "Teacher";
		}
		if ( listFind( userGroups, "P002", "," ) ) {
			userType = "Student";
		}
		
		stuId = event.getArg("STU");
		type = event.getArg("STR");
		
		/* 尝试自动更新学生照片信息 */
		uploadFile = getProperty("cdnAssetsPath") & "/tmp/stuPicture/" & stuId & ".jpg";

		if ( fileExists(uploadFile) ) {

			portraitDir = getProperty("cdnAssetsPath") & "/student/portrait/" & left( stuId, 4 ) & "/";
			profileDir = getProperty("cdnAssetsPath") & "/student/profile/" & left( stuId, 4 ) & "/";
			printDir = getProperty("cdnAssetsPath") & "/student/print/" & left( stuId, 4 ) & "/";
					
			if ( not directoryExists( portraitDir ) ) {
				directoryCreate( portraitDir );
			}
			if ( not directoryExists( profileDir ) ) {
				directoryCreate( profileDir );
			}
			if ( not directoryExists( printDir ) ) {
				directoryCreate( printDir );
			}

			/* 如果有上传的待处理照片 则自动生成各规格图片 */

			/* 小头像 50x50 */
			if ( fileExists( portraitDir & stuId & ".jpg" ) ) {
				fileObj.delete( portraitDir & stuId & ".jpg" );
			}
			
			imgObj.readImage( uploadFile );

			imgObj.scaleWidth( 152 );
			imgObj.blur( 1 );
			imgObj.crop( 1, max( (imgObj.getHeight()-150)/2 - 15, 1 ), 150, 150 );
			imgObj.scaleWidth( 50 );
			imgObj.writeImage( portraitDir & stuId & ".jpg", "jpg", 100);
			
			/* 将图片对象放置到系统缓存 */
			binaryStream = fileObj.readBinary( portraitDir & stuId & ".jpg" );
			cachePut( "user_portrait_" & stuId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );

			/* 证件照 135x180 */
			if ( fileExists( profileDir & stuId & ".jpg" ) ) {
				fileObj.delete( profileDir & stuId & ".jpg" );
			}

			imgObj.readImage( uploadFile );

			imgObj.scaleWidth( 405 );
			imgObj.blur( 1 );
			imgObj.scaleWidth( 137 );
			imgObj.crop( 1, (imgObj.getHeight()-180)/2, 135, 180 );
			imgObj.writeImage( profileDir & stuId & ".jpg", "jpg", 100);
			
			/* 将图片对象放置到系统缓存 */
			binaryStream = fileObj.readBinary( profileDir & stuId & ".jpg" );
			cachePut( "user_profile_" & stuId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
			
			
			/* 打印照片 480x640 */
			if ( fileExists( printDir & stuId & ".jpg" ) ) {
				fileObj.delete( printDir & stuId & ".jpg" );
			}

			imgObj.readImage( uploadFile );

			imgObj.scaleWidth( 484 );
			imgObj.crop( 2, (imgObj.getHeight()-640)/2, 480, 640 );
			imgObj.writeImage( printDir & stuId & ".jpg", "jpg", 100);
			
			/* 将图片对象放置到系统缓存 */
			binaryStream = fileObj.readBinary( printDir & stuId & ".jpg" );
			cachePut( "user_print_" & stuId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
			
			/* 删除临时文件 */
			fileDelete( uploadFile );

		}
		
		switch (type) {
			
			case "portrait":
				
				/* 50x50 */
				target = getProperty("cdnAssetsPath") & "/profile/default_s.jpg";
				
				binaryStream = cacheGet( "user_portrait_default", "matrixPicture" );
				
				if ( isNull(binaryStream) ) {
					binaryStream = fileObj.readBinary( target );
					/* 将默认文件对象放置到系统缓存 */
					cachePut( "user_portrait_default", binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
				}
				
				if ( userType eq "Student") {
					
					binaryStream = cacheGet( "user_portrait_" & userId, "matrixPicture" );
					
					if ( isNull(binaryStream) ) {

						newTarget = getProperty("cdnAssetsPath") & "/student/portrait/" & left( userId, 4 ) & "/" & userId & ".jpg";
					
						if ( fileExists(newTarget) ) {
							target = newTarget;
						}
						
						binaryStream = fileObj.readBinary( target );
						/* 将图片对象放置到系统缓存 */
						cachePut( "user_portrait_" & userId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
						
					}

				}
				
				if ( userType eq "Teacher") {
					
					binaryStream = cacheGet( "user_portrait_" & stuId, "matrixPicture" );
					
					if ( isNull(binaryStream) ) {
					
						newTarget = getProperty("cdnAssetsPath") & "/student/portrait/" & left( stuId, 4 ) & "/" & stuId & ".jpg";
					
						if ( fileExists(newTarget) ) {
							target = newTarget;
						}
						
						binaryStream = fileObj.readBinary( target );
						/* 将图片对象放置到系统缓存 */
						cachePut( "user_portrait_" & stuId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
					}

				}
				
				
				break;
			
			case "profile":
				
				/* 135x180 */
				target = getProperty("cdnAssetsPath") & "/profile/default.jpg";
				
				binaryStream = cacheGet( "user_profile_default", "matrixPicture" );
				
				if ( isNull(binaryStream) ) {
					binaryStream = fileObj.readBinary( target );
					/* 将默认文件对象放置到系统缓存 */
					cachePut( "user_profile_default", binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
				}

				if ( userType eq "Student") {
					
					binaryStream = cacheGet( "user_profile_" & userId, "matrixPicture" );
					
					if ( isNull(binaryStream) ) {
					
						newTarget = getProperty("cdnAssetsPath") & "/student/profile/" & left( userId, 4 ) & "/" & userId & ".jpg";
					
						if ( fileExists(newTarget) ) {
							target = newTarget;
						}
						
						binaryStream = fileObj.readBinary( target );
						/* 将图片对象放置到系统缓存 */
						cachePut( "user_profile_" & userId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
					
					}
					
				}
				
				if ( userType eq "Teacher") {
					
					binaryStream = cacheGet( "user_profile_" & stuId, "matrixPicture" );
					
					if ( isNull(binaryStream) ) {
						
						newTarget = getProperty("cdnAssetsPath") & "/student/profile/" & left( stuId, 4 ) & "/" & stuId & ".jpg";
					
						if ( fileExists(newTarget) ) {
							target = newTarget;
						}
						
						binaryStream = fileObj.readBinary( target );
						/* 将图片对象放置到系统缓存 */
						cachePut( "user_profile_" & stuId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
						
					}

				}

				break;
			
			case "print":
				
				/* 480x640 */
				target = getProperty("cdnAssetsPath") & "/profile/default_p.jpg";

				binaryStream = cacheGet( "user_print_default", "matrixPicture" );
				
				if ( isNull(binaryStream) ) {
					binaryStream = fileObj.readBinary( target );
					/* 将默认文件对象放置到系统缓存 */
					cachePut( "user_print_default", binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
				}
				
				if ( userType eq "Student") {
					
					binaryStream = cacheGet( "user_print_" & userId, "matrixPicture" );
					
					if ( isNull(binaryStream) ) {
					
						newTarget = getProperty("cdnAssetsPath") & "/student/print/" & left( userId, 4 ) & "/" & userId & ".jpg";
					
						if ( fileExists(newTarget) ) {
							target = newTarget;
						}
						
						binaryStream = fileObj.readBinary( target );
						/* 将图片对象放置到系统缓存 */
						cachePut( "user_print_" & userId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
						
					}
					
				}
				
				if ( userType eq "Teacher") {
					
					binaryStream = cacheGet( "user_print_" & stuId, "matrixPicture" );
					
					if ( isNull(binaryStream) ) {
					
						newTarget = getProperty("cdnAssetsPath") & "/student/print/" & left( stuId, 4 ) & "/" & stuId & ".jpg";
					
						if ( fileExists(newTarget) ) {
							target = newTarget;
						}
						
						binaryStream = fileObj.readBinary( target );
						/* 将图片对象放置到系统缓存 */
						cachePut( "user_print_" & stuId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
						
					}
					
				}
				
				break;
			
		}
		
		contentObj.contentWriteThrough( "image/jpeg", binaryStream, true );

	</cfscript>
</cfsilent>